package com.kingtone.rongji.ui;

import com.handmark.pulltorefresh.library.PullToRefreshBase.State;
import com.kingtone.rongji.tool.DensityUtil;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;

public class ProductScrollView extends ScrollView {

	View mTopView;
	
	View mFlowView;

	private OnLoadingListener mOnRefreshListener;

	private boolean isLoading = false;
	
	public ProductScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		super.onScrollChanged(l, t, oldl, oldt);
	}
	
	private float mLastMotionY;
	
	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		if (event.getAction() == MotionEvent.ACTION_DOWN && event.getEdgeFlags() != 0) {
			return false;
		}

		switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN: {
				mLastMotionY = event.getY();
				break;
			}
			case MotionEvent.ACTION_CANCEL:
			case MotionEvent.ACTION_UP: {
				float t =  mLastMotionY - event.getY();
				if(t > 100 && !isLoading)
				{
					mOnRefreshListener.startLoading();
					isLoading = true;
				}
				break;
			}
		}
		return super.onTouchEvent(event);
	}
	
	public void listenerFlowViewScrollState(View topView, View flowView) {
		mTopView = topView;
		mFlowView = flowView;
	}
	
	public void setOnLoadingListener(OnLoadingListener listener) {
		mOnRefreshListener = listener;
	}
	
	public static interface OnLoadingListener{
		public void startLoading();
	}
}
